package org.jeecg.modules.nurseuser_profile.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nurse_hospital.entity.NurseHospital;
import org.jeecg.modules.nurse_hospital.service.INurseHospitalService;
import org.jeecg.modules.nurseuser_profile.entity.NurseuserProfile;
import org.jeecg.modules.nurseuser_profile.service.INurseuserProfileService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 医护端护工报名表
 * @Author: jeecg-boot
 * @Date:   2025-08-30
 * @Version: V1.0
 */
@Api(tags="医护端护工报名表")
@RestController
@RequestMapping("/nurseuser_profile/nurseuserProfile")
@Slf4j
public class NurseuserProfileController extends JeecgController<NurseuserProfile, INurseuserProfileService> {
	@Autowired
	private INurseuserProfileService nurseuserProfileService;
	 @Autowired
	 private ISysUserService sysUserService;
	 @Autowired
	 private INurseHospitalService nurseHospitalService;
	
	/**
	 * 分页列表查询
	 *
	 * @param nurseuserProfile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "医护端护工报名表-分页列表查询")
	@ApiOperation(value="医护端护工报名表-分页列表查询", notes="医护端护工报名表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<NurseuserProfile>> queryPageList(NurseuserProfile nurseuserProfile,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<NurseuserProfile> queryWrapper = QueryGenerator.initQueryWrapper(nurseuserProfile, req.getParameterMap());
		Page<NurseuserProfile> page = new Page<NurseuserProfile>(pageNo, pageSize);
		IPage<NurseuserProfile> pageList = nurseuserProfileService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 
	 /**
	  * 分页列表查询
	  *
	  * @param nurseuserProfile
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "医护端护工报名表-分页列表查询")
	 @ApiOperation(value="医护端护工报名表-分页列表查询", notes="医护端护工报名表-分页列表查询")
	 @GetMapping(value = "/listAll")
	 public Result<IPage<NurseuserProfile>> queryPageListAll(NurseuserProfile nurseuserProfile,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  HttpServletRequest req) {
		 QueryWrapper<NurseuserProfile> queryWrapper = QueryGenerator.initQueryWrapper(nurseuserProfile, req.getParameterMap());
		 Page<NurseuserProfile> page = new Page<NurseuserProfile>(pageNo, pageSize);
		 SysUser sysuser= sysUserService.getLoginUser();
		 List<NurseHospital> list = nurseHospitalService.list(new LambdaQueryWrapper<NurseHospital>().eq(NurseHospital::getSysOrgCode,sysuser.getOrgCode()));
		 if(!list.isEmpty()){
			 queryWrapper.eq("hospital",list.get(0).getId());
		 }
		 IPage<NurseuserProfile> pageList = nurseuserProfileService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param nurseuserProfile
	 * @return
	 */
	@AutoLog(value = "医护端护工报名表-添加")
	@ApiOperation(value="医护端护工报名表-添加", notes="医护端护工报名表-添加")
	@RequiresPermissions("nurseuser_profile:nurseuser_profile:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody NurseuserProfile nurseuserProfile) {
		nurseuserProfileService.save(nurseuserProfile);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param nurseuserProfile
	 * @return
	 */
	@AutoLog(value = "医护端护工报名表-编辑")
	@ApiOperation(value="医护端护工报名表-编辑", notes="医护端护工报名表-编辑")
//	@RequiresPermissions("nurseuser_profile:nurseuser_profile:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody NurseuserProfile nurseuserProfile) {
		nurseuserProfileService.updateById(nurseuserProfile);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "医护端护工报名表-通过id删除")
	@ApiOperation(value="医护端护工报名表-通过id删除", notes="医护端护工报名表-通过id删除")
	@RequiresPermissions("nurseuser_profile:nurseuser_profile:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		nurseuserProfileService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "医护端护工报名表-批量删除")
	@ApiOperation(value="医护端护工报名表-批量删除", notes="医护端护工报名表-批量删除")
	@RequiresPermissions("nurseuser_profile:nurseuser_profile:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.nurseuserProfileService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "医护端护工报名表-通过id查询")
	@ApiOperation(value="医护端护工报名表-通过id查询", notes="医护端护工报名表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<NurseuserProfile> queryById(@RequestParam(name="id",required=true) String id) {
		NurseuserProfile nurseuserProfile = nurseuserProfileService.getById(id);
		if(nurseuserProfile==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(nurseuserProfile);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param nurseuserProfile
    */
    @RequiresPermissions("nurseuser_profile:nurseuser_profile:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NurseuserProfile nurseuserProfile) {
        // 使用queryPageListAll的查询逻辑进行导出，但使用page方法查询
        QueryWrapper<NurseuserProfile> queryWrapper = QueryGenerator.initQueryWrapper(nurseuserProfile, request.getParameterMap());
        SysUser sysuser = sysUserService.getLoginUser();
		String name=sysuser!=null?sysuser.getRealname():"admin";
//        List<NurseHospital> list = nurseHospitalService.list(new LambdaQueryWrapper<NurseHospital>().eq(NurseHospital::getSysOrgCode, sysuser.getOrgCode()));
//        if (!list.isEmpty()) {
//            queryWrapper.eq("hospital", list.get(0).getId());
//        }
        
        // 过滤选中数据
//        String selections = request.getParameter("selections");
//        if (oConvertUtils.isNotEmpty(selections)) {
//            List<String> selectionList = Arrays.asList(selections.split(","));
//            queryWrapper.in("id", selectionList);
//        }
        
        // 使用page方法查询所有数据（设置一个足够大的pagesize）
        Page<NurseuserProfile> page = new Page<>(1, Integer.MAX_VALUE);
        IPage<NurseuserProfile> pageList = nurseuserProfileService.page(page, queryWrapper);
        List<NurseuserProfile> exportList = pageList.getRecords();
        
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "医护端护工报名表");
        mv.addObject(NormalExcelConstants.CLASS, NurseuserProfile.class);
        ExportParams exportParams = new ExportParams("医护端护工报名表" + "报表", "导出人:" + name, "医护端护工报名表");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("nurseuser_profile:nurseuser_profile:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NurseuserProfile.class);
    }

}